package org.steven.crm.core.utils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author wuzw
 * @Date 2023/6/28 13:46
 * @Version 1.0
 */
public class TimeUtil {
    public static String getMinutesBefore(Integer minutes) {
        return getTimeBefore(Long.parseLong((minutes * 60 * 1000 + "")));
    }

    public static String getMinutesBefore(String fromTime, Integer minutes) throws Exception {
        return getTimeBefore(fromTime, Long.parseLong((minutes * 60 * 1000 + "")));
    }

    /**
     * 计算两个时间的差，返回相差的毫秒数
     * @param time1
     * @param time2
     * @param format
     * @return
     * @throws Exception
     */
    public static long getTimeDiffAbs(String time1, String time2, String format) throws Exception {
        return Math.abs(toTimestamp(time1, format) - toTimestamp(time2, format));
    }

    public static long getTimeDiff(String time1, String time2, String format) throws Exception {
        return toTimestamp(time1, format) - toTimestamp(time2, format);
    }

    public static String getTimeBefore(Long time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = new Date();

        Date afterDate = new Date(now.getTime() - time);
        return sdf.format(afterDate);
    }

    public static String getTimeBefore(String fromTime, Long time) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = sdf.parse(fromTime);

        Date afterDate = new Date(now.getTime() - time);
        return sdf.format(afterDate);
    }

    public static long toTimestamp(String dateTime, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        Date date = sdf.parse(dateTime);

        return date.getTime();
    }

    public static long calDiffTimestamp(String endDateTime, String fromDateTime, String format) throws Exception {
        return toTimestamp(endDateTime, format) - toTimestamp(fromDateTime, format);
    }


    public static String getDateBefore(String date, String format, int type, int num)
            throws  Exception {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        now.setTime(simpleDateFormat.parse(date));
        now.set(type, now.get(type) - num);
        return simpleDateFormat.format(now.getTime());
    }

    public static String getDateAfter(String date, String format, int type, int num)
            throws  Exception {
        Calendar now = Calendar.getInstance();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        now.setTime(simpleDateFormat.parse(date));
        now.set(type, now.get(type) + num);
        return simpleDateFormat.format(now.getTime());
    }

    public static int getWeekDay(String dateStr) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateStr);

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);

        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    /**
     * 获取从指定日期开始，往后days天的日期
     * @param fromDate
     * @param days
     * @return
     */
    public static List<String> getRecentDates(String fromDate, int days) throws Exception {
        List<String> dates = new ArrayList<>();
        dates.add(fromDate);

        for (int i = 0; i < days; ++i) {
            dates.add(getDateAfter(fromDate, "yyyy-MM-dd", Calendar.DATE, i + 1));
        }

        return dates;
    }

    public static String parseTimeMillToDateTime(String longTimeMill) {
        if(longTimeMill == null || "".equals(longTimeMill)) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.parseLong(longTimeMill));
        return sdf.format(calendar.getTime());
    }

    public static List<String> getHourRange(String startDateStr, String endDateStr) throws Exception {
        List<String> hourList = new ArrayList<>();
        SimpleDateFormat hourFormat = new SimpleDateFormat("yyyy-MM-dd HH");

        Date startDate = hourFormat.parse(startDateStr);
        Date endDate = hourFormat.parse(endDateStr);

        Date currentHour = startDate;
        while (currentHour.compareTo(endDate) <= 0) {
            hourList.add(hourFormat.format(currentHour));
            currentHour.setTime(currentHour.getTime() + 60 * 60 * 1000);
        }

        return hourList;
    }

    public static Map<String, List<String>> getDateHours(String startTime, String endTime) throws Exception {
        Map<String, List<String>> dateHourMap = new HashMap<>();
        List<String> dateHours = TimeUtil.getHourRange(startTime, endTime);
        for (String dateHour : dateHours) {
            String date = dateHour.substring(0, 10);
            String hour = dateHour.substring(11);
            if (hour.length() > 1 && hour.startsWith("0")) {
                hour = hour.substring(1);
            }

            if (!dateHourMap.containsKey(date)) {
                dateHourMap.put(date, new ArrayList<>());
            }
            dateHourMap.get(date).add(hour);
        }

        return dateHourMap;
    }

    public static List<String> getDates(String startDate, String endDate, String format) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat(format);

        Date start = sdf.parse(startDate);
        Date end = sdf.parse(endDate);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(start);
        List<String> dates = new ArrayList<>();
        while (calendar.getTime().getTime() <= end.getTime()) {
            String str = sdf.format(calendar.getTime());
            dates.add(str);
            calendar.add(Calendar.DAY_OF_MONTH, 1);
        }
        return dates;
    }

    public static void main(String[] args) throws Exception {
        System.out.println(getDates("2023-11-21", "2023-11-22", "yyyy-MM-dd"));
    }
}
